package cpcn.dsp.demo.tx.corpquery;


import cpcn.dsp.demo.consts.DSPConsts;
import cpcn.dsp.demo.util.HttpsUtil;
import cpcn.dsp.institution.api.security.EncryptAndDecrypt;
import cpcn.dsp.institution.api.system.DSPInstitutionEnvironment;
import cpcn.dsp.institution.api.tx.corpquery.Tx1304Request;
import cpcn.dsp.institution.api.tx.corpquery.Tx1304Response;
import cpcn.dsp.institution.api.util.Base64;
import cpcn.dsp.institution.api.util.GUIDGenerator;

import java.util.HashMap;
import java.util.Map;


/**
 * @ClassName: Test1304
 * @Description: 以同步接口1304为例进行接口调用
 * @Author: chengzi
 * @Date: 2020/7/2 0002 13:02
 * @Version: v1.0 文件初始创建
 */
public class Test1304 {
    public static void main(String[] args) throws Exception {

        //1.初始化
        DSPInstitutionEnvironment.initialize(DSPConsts.Keystore_test, DSPConsts.myKeystorePassword_test, DSPConsts.publicKey_test, DSPConsts.MSG_ENCRYPT_20);
        //1.1 encryptSN、signSN值不为空，说明初始化证书成功
        System.out.println(DSPInstitutionEnvironment.encryptSN);
        System.out.println(DSPInstitutionEnvironment.signSN);

        //2.生成请求明文
        Tx1304Request tx1304Request = new Tx1304Request();
        tx1304Request.setInstitutionID("100827");// 机构号由中金分配
        tx1304Request.setTxSN(GUIDGenerator.genGUID());
        tx1304Request.setOrgIdentificationType("20");
        tx1304Request.setOrganizationName("****有限公司");
        tx1304Request.setOrgIdentificationNumber("91110102551443078Q");
        tx1304Request.setName("张三");
        tx1304Request.setIdentificationType("0");
        tx1304Request.setIdentificationNumber("110101198001010037");
        tx1304Request.setRemark("1");//仅测试环境判断该字段1->不一致,2->未知，3->请求失败，其他->一致

        //报文处理
        tx1304Request.process();
        String plainText = tx1304Request.getRequestPlainText();
        System.out.println("请求明文：" + plainText);

        String reqMessage = tx1304Request.getRequestMessage();
        String reqSignature = tx1304Request.getRequestSignature();
        String requestDgtlEnvlp = tx1304Request.getRequestDgtlEnvlp();
        String requestSignSN = tx1304Request.getRequestSignSN();
        String requestEncryptSN = tx1304Request.getRequestEncryptSN();
        //生成参数
        Map<String, String> paramMap = new HashMap<String, String>();
        paramMap.put("message", reqMessage);
        paramMap.put("signature", reqSignature);
        paramMap.put("dgtlenvlp", requestDgtlEnvlp);
        paramMap.put("signSN", requestSignSN);
        paramMap.put("encryptSN", requestEncryptSN);
        //生成请求参数
        String requestString = EncryptAndDecrypt.createLinkStringByGet(paramMap);
        System.out.println("请求密文：" + requestString);
        //请求接口
        String result = HttpsUtil.post(DSPConsts.TX_URL_TEST, requestString, null);
        System.out.println("响应密文：" + result);

        String respPlainText = null;
        String[] respMsg = result.split(",");
        if (respMsg.length == 1 && -1 == respMsg[0].indexOf(',')) {
            respPlainText = Base64.decode(respMsg[0], "UTF-8");
        } else if (respMsg.length == 5) {
            String responseMessage = respMsg[0];
            ;
            String responseSignature = respMsg[1];
            ;
            String responseDgtlEnvlp = respMsg[2];
            ;
            String respSignSN = respMsg[3];
            ;
            String respEncryptSN = respMsg[4];
            ;

            Tx1304Response tx1304Response = new Tx1304Response(responseMessage, responseSignature, responseDgtlEnvlp, respSignSN, respEncryptSN);
            respPlainText = tx1304Response.getResponsePlainText();
            if ("2000".equals(tx1304Response.getCode())) {
                String verification = tx1304Response.getVerification();
                System.out.println("验证结果" + verification);
            }
        }
        System.out.println("响应明文：" + respPlainText);


    }
}
